#!/usr/bin/python
# Project: Soar <http://soar.googlecode.com>
# Author: Mazin Assanie

import sys
import os
import SCons.Script

Import('env', 'InstallDir')
clone = env.Clone()

# Set up variables (some are hardcoded for now)
libname = 'tclsoarlib'

# Set up includes variables
if sys.platform == 'win32':
    tcl_platform_dir = os.path.join('C:/Tcl')

    tcl_user_platform_dir = GetOption('tcl')

    if tcl_user_platform_dir:
        tcl_platform_dir = tcl_user_platform_dir

    tcl_libs = clone.Dir(os.path.join(tcl_platform_dir, 'lib'))
    tcl_headers = clone.Dir(os.path.join(tcl_platform_dir, 'include'))
    tcl_bins = clone.Dir(os.path.join(tcl_platform_dir, 'bin'))
    tcl_libname = 'tcl86.lib'
    tcl_lib = 'tcl86'
elif sys.platform == 'darwin':
    tcl_platform_dir = os.path.join('/Library/Frameworks/Tcl.framework/Versions/Current')

    tcl_user_platform_dir = GetOption('tcl')

    if tcl_user_platform_dir:
        tcl_platform_dir = tcl_user_platform_dir

    tcl_libs = clone.Dir(tcl_platform_dir)
    tcl_headers = clone.Dir(os.path.join(tcl_platform_dir, 'Headers'))
    tcl_bins = clone.Dir(tcl_platform_dir)
    tcl_libname = 'Tcl'
    tcl_lib = 'Tcl'
else:
    tcl_platform_dir = os.path.join('/opt/ActiveTcl-8.6')

    tcl_user_platform_dir = GetOption('tcl')

    if tcl_user_platform_dir:
        tcl_platform_dir = tcl_user_platform_dir

    tcl_libs = clone.Dir(os.path.join(tcl_platform_dir, 'lib'))
    tcl_headers = clone.Dir(os.path.join(tcl_platform_dir, 'include'))
    tcl_bins = clone.Dir(os.path.join(tcl_platform_dir, 'bin'))
    tcl_libname = 'libtcl8.6.so'
    tcl_lib = 'tcl8.6'

if ((not tcl_libs.exists()) or (not tcl_headers.exists()) or
    (not File(os.path.join(tcl_headers.abspath, 'tcl.h')).exists()) or
    (not File(os.path.join(tcl_libs.abspath, tcl_libname)).exists())) :
    print 'Tcl headers and libraries not found at ' + tcl_platform_dir + ', not building TclSoarLib.'
    Return()
else:
    print 'TclSoarLib is buildable.'

# A better way?
#
# conf = Configure(env)
# print "Checking! ",
# if not conf.CheckLibWithHeader('Tcl', 'tcl.h', 'c'):
#     print 'Did not find Tcl!!\n\n'
# if not conf.CheckLibWithHeader('Tcl8.5', 'tcl.h', 'c'):
#     print 'Did not find Tcl 8.5!!\n\n'

include_dirs = [ clone.Dir(('#Core/%s/src') % d).abspath for d in 'ElementXML'.split() ]
includes = ' '.join(' -I%s' % d for d in include_dirs)
cpps = ' '.join(' %s' % d for d in include_dirs)

clone.Append(CXXFLAGS = Split('-I' + tcl_headers.abspath + ' -I' + includes))
clone.Append(CPPPATH = Split(tcl_headers.abspath + ' ' + cpps))
clone.Append(LIBPATH = [tcl_libs.abspath])
clone.Append(CPPFLAGS = ['-w'])

# Set up compiler flags
if os.name == 'posix':
  clone.Append(CXXFLAGS = Split('-Wno-unused -fno-strict-aliasing'))

#-fPic is needed to make the code position independent.  Necessary for Tcl.
if sys.platform == 'darwin':
  # Link error occurs if we include the -bundle flag with -flat_namespace, so we removed it
  clone.Append(SHLINKFLAGS=Split('$LINKFLAGS -flat_namespace -undefined suppress -fmessage-length=0'))
  install_name = os.path.join('@loader_path', env['LIBPREFIX'] + 'TclSoarLib' + env['SHLIBSUFFIX'])
  clone.Append(LINKFLAGS = ['-install_name', install_name])
  clone.Append(LINKFLAGS = ['-framework', tcl_lib])
elif sys.platform == 'linux2':
  clone.Append(CPPFLAGS = ['-fPIC'])
  clone.Append(LIBS = [tcl_lib, 'Soar'])
  # Currently requires Tcl 8.6 b/c it supports multiple threads without manual compilation.  The following line
  # hard-codes the path of the current Tcl 8.6 path on this system into the executable
  clone.Append(LINKFLAGS = ['-Wl,-rpath,\''+tcl_libs.abspath+'\''])
elif os.name == 'nt':
  # Windows DLLs need to get linked to dependencies, whereas Linux and Mac shared objects do not
  # (not sure if this is really needed for TclSoarLib)
  clone.Append(LIBS = ['advapi32'])
  clone.Append(LIBS = [tcl_lib, 'Soar'])

# Copy tcl scripts that library needs
tcl_scripts = InstallDir(clone, env['OUT_DIR'] + '/tcl', 'tcl')

# Create the TclSoarLib dll and tclindex file
lib = clone.SharedLibrary(libname, Glob('src/*.cpp'))[0]

# Install TclSoarLib and return
t = clone.Install(clone.Dir(env['OUT_DIR']), lib)
clone.Alias('tclsoarlib', [t, tcl_scripts])
